<?php
error_reporting(E_ALL);
require __DIR__.'/../Connect.php';

use PhpAmqpLib\Message\AMQPMessage;

class Task
{

    public $exchange = 'direct_exchange';



    public function send()
    {
        $connect = Connect::getInstance();
        $channel = $connect->channel();


        $channel->set_ack_handler(function (AMQPMessage $message){
            echo 'ack' . $message->getBody() . PHP_EOL;
        });
        //nack callback function
        $channel->set_nack_handler(function (AMQPMessage $message){
            echo 'nack' . $message->getBody() .PHP_EOL;
        });

        //$channel->exchange_declare($this->exchange,'fanout',false,true);
        //$channel->queue_declare('task_queue', false,false,false,false);
        $msg = 'Hello world.'.date('Y-m-d H:i:s').rand(1,100);
        $msgObj = new AMQPMessage($msg, ['delivery_mode' => AMQPMessage::DELIVERY_MODE_PERSISTENT]);

        $channel->confirm_select(false);
        $channel->basic_publish($msgObj,$this->exchange,'email');
        $channel->wait_for_pending_acks_returns(5);

        $channel->close();
        $connect->close();
        echo '信息发送成功'.PHP_EOL;
    }
}
(new Task())->send();
